<template>
  <div class="reportFormPage">
    <topOperation ref="TopOperation" :tabs-list="tabList" :active-val.sync="activeVal" @currentActive="currentActive"
      @reportFormsSubmit="reportFormsSubmit" @printSpecificPart="onPrintSpecificPart" />
    <div ref="printView" v-loading="modPagesLodding">
      <!--   沥青针入度、延度、软化点试验检测记录表 -->
      <div v-show="activeVal === 'zrdydrhdjlbrh'" v-for="(item, index) in excelTypeMap['zrdydrhdjlbrh']"
        :key="'zrdydrhdjlbrh' + index">
        <Zrdydrhdjlbrh ref="zrdydrhdjlbrh" :excel-id="item" :page="index" :syType="form.syType" />
      </div>
      <!--   乳化沥青蒸发残留物含量试验检测记录表 -->
      <div v-show="activeVal === 'rhlqclwjlb'" v-for="(item, index) in excelTypeMap['rhlqclwjlb']"
        :key="'rhlqclwjlb' + index">
        <Rhlqclwjlb ref="rhlqclwjlb" :excel-id="item" :page="index" :syType="form.syType" />
      </div>
      <!--   乳化沥青与粗集料的黏附性试验检测记录表 -->
      <div v-show="activeVal === 'rhlqcjlnfxjlb'" v-for="(item, index) in excelTypeMap['rhlqcjlnfxjlb']"
        :key="'rhlqcjlnfxjlb' + index">
        <Rhlqcjlnfxjlb ref="rhlqcjlnfxjlb" :excel-id="item" :page="index" :syType="form.syType" />
      </div>
      <!--   乳化沥青筛上剩余量、微粒离子电荷试验检测记录表 -->
      <div v-show="activeVal === 'rhlqdhjlb'" v-for="(item, index) in excelTypeMap['rhlqdhjlb']"
        :key="'rhlqdhjlb' + index">
        <Rhlqdhjlb ref="rhlqdhjlb" :excel-id="item" :page="index" :syType="form.syType" />
      </div>
      <!--   乳化沥青储存稳定性、破乳速度试验检测记录表（一） -->
      <div v-show="activeVal === 'rhlqwdxjlb'" v-for="(item, index) in excelTypeMap['rhlqwdxjlb']"
        :key="'rhlqwdxjlb' + index">
        <Rhlqwdxjlb ref="rhlqwdxjlb" :excel-id="item" :page="index" :syType="form.syType" />
      </div>
      <!--   道路用乳化沥青试验检测报告 -->
      <div v-show="activeVal === 'rhlqbg'" v-for="(item, index) in excelTypeMap['rhlqbg']" :key="'rhlqbg' + index">
        <Rhlqbg ref="rhlqbg" :excel-id="item" :page="index" :syType="form.syType" />
      </div>
    </div>
    <backtop-view :sum="activeVal ? excelTypeMap[activeVal].length : 1" @onModPages="onModPages" />
    <statement-select-view ref="statementSelectView" :data-list="tabList" @onShowPreview="onShowPreview" />
    <statement-select-dialog ref="statementSelectDialog" />
  </div>
</template>

<script>
import Bus from "@/utils/bus";
import store from "@/store";
import { bsDataReportModPages, bsDataReportTb, getBsDataReport } from "@/api/syrw/bsDataReport";
import StatementSelectView from '@/views/reportForms/components/StatementSelectView.vue';
import StatementSelectDialog from "@/views/reportForms/components/StatementSelectDialog.vue";
export default {
  name: "index2",
  data () {
    return {
      modPagesLodding: false,
      reportId: undefined,
      tabList: [],
      form: {
        syType: "",
        excelTypeList: {}
      },
      excelTypeMap: [],
      activeVal: "",
    };
  },
  components: {
    TopOperation: () => import("@/views/reportForms/components/topOperation"), // tab页
    BacktopView: () => import("@/views/reportForms/components/backtopView"), // 回到顶部页
    Zrdydrhdjlbrh: () => import("@/views/reportForms/lqn/rh/zrdydrhdjlbrh"), // 沥青针入度、延度、软化点试验检测记录表 zrdydrhdjlbrh
    Rhlqclwjlb: () => import("@/views/reportForms/lqn/rh/rhlqclwjlb"), // 乳化沥青蒸发残留物含量试验检测记录表 rhlqclwjlb
    Rhlqcjlnfxjlb: () => import("@/views/reportForms/lqn/rh/rhlqcjlnfxjlb"), // 乳化沥青与粗集料的黏附性试验检测记录表 rhlqcjlnfxjlb
    Rhlqdhjlb: () => import("@/views/reportForms/lqn/rh/rhlqdhjlb"), // 乳化沥青筛上剩余量、微粒离子电荷试验检测记录表 rhlqdhjlb
    Rhlqwdxjlb: () => import("@/views/reportForms/lqn/rh/rhlqwdxjlb"), // 乳化沥青储存稳定性、破乳速度试验检测记录表（一） rhlqwdxjlb
    Rhlqbg: () => import("@/views/reportForms/lqn/rh/rhlqbg"), // 道路用乳化沥青试验检测报告 rhlqbg
    StatementSelectView,
    StatementSelectDialog
  },
  watch: {
    'activeVal' (val) {
      this.reportFormsSubmit(false);
    }
  },
  created () {
    if (this.$route.query) {
      this.reportId = this.$route.query.reportId;
    }
    this.init();
  },
  mounted () {
    // 填充特殊符号
    Bus.$on("setCharacter", (data) => {
      const { focusInput, page } = store.state.reportForms;
      this.$refs[this.activeVal][page].form[focusInput] = (this.$refs[this.activeVal][page].form[focusInput] || "") + data;
    });
  },
  beforeDestroy () {
    // 停止监听事件
    Bus.$off("setCharacter");
  },
  methods: {
    // 获取记录表和报告表
    async init () {
      if (this.reportId) {
        getBsDataReport(this.reportId).then((response) => {
          this.form = response.data;
          this.tabList = [];
          this.excelTypeMap = [];
          if (!this.form.excelTypeList || this.form.excelTypeList.length == 0) {
            this.form.excelTypeList.push({
              val: undefined,
              name: undefined
            });
            this.tabList = this.form.excelTypeList;
          } else {
            this.form.excelTypeList.forEach((item) => {
              if (this.excelTypeMap.hasOwnProperty(item.label)) {
                this.excelTypeMap[item.label].push(item.excelId);
              } else {
                this.excelTypeMap[item.label] = [item.excelId];
                this.tabList.push({
                  val: item.label,
                  name: item.excelTypeName
                });
              }
            });
          }
          this.activeVal = this.tabList[0].val;

          // this.currentActive(this.activeVal)
        });
      } else {
        this.$message.error("报告id不能为空");
      }
    },
    currentActive (val) {
      this.activeVal = val;
    },
    // 加一页减一页
    async onModPages (modType) {
      this.modPagesLodding = true;
      // 1先保存
      await this.reportFormsSubmit();
      // 2 加减页
      await this.doModPages(modType);
      // 3 重新加载
      await this.init();
      // 4 刷新
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            this.$refs[item.val][index].init();
          }
        });
      });
      this.modPagesLodding = false;
    },
    // 批量提交
    async reportFormsSubmit (hasTips = true) {
      let arr = [];
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            let info = this.$refs[item.val][index].submitForm();
            arr.push({ label: item.val, excelObj: info });
          }
        });
      });
      await bsDataReportTb(arr);
      if (hasTips) {
        this.$modal.msgSuccess("操作成功");
      };
    },
    // 加减页
    async doModPages (modType) {
      const arr = this.excelTypeMap[this.activeVal];
      const data = {
        reportId: this.reportId,
        label: this.activeVal,
        modType: modType,
        excelId: Number(modType) < 0 ? arr[arr.length - 1] : undefined
      };
      await bsDataReportModPages(data);
    },
    // 选择报表
    onPrintSpecificPart () {
      this.$refs.statementSelectView.init();
    },
    //预览
    onShowPreview (tableList) {
      if (!tableList || tableList.length <= 0) {
        return;
      }
      let arr = [];
      this.form.excelTypeList.forEach((item) => {
        if (tableList.includes(item.label)) {
          arr.push({ label: item.label, excelId: item.excelId, syType: this.form.syType, typeSort: item.excelIndex });
        }
      });
      this.$refs.statementSelectDialog.init(arr);
    },
  }
};
</script>

<style scoped lang="scss">
//@import url('~@/views/reportForms/style/reportForm.scss');</style>
